package com.zhl.study.annotation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @description 自定义校验规则
 * @Author: z156940l@outlook.com
 * @className MyConstraintValidator
 * @Date: 2023/4/28 14:44
 */
public class MyConstraintValidator implements ConstraintValidator<ArrayVal, Integer> {

    private Set<Integer> set = new HashSet<>();

    @Override
    public void initialize(ArrayVal constraintAnnotation){
        int[] value = constraintAnnotation.value();
        for (int i : value) {
            set.add(i);
        }
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
